package cn.wolfcode.controller;


import cn.wolfcode.domain.Notice;
import cn.wolfcode.mapper.NoticeReadMapper;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;
    @Autowired
    private NoticeReadMapper readMapper;


    @RequiredPermission(name = "公告页面", expression = "notice:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo) {
        model.addAttribute("user", UserContext.getCurrentUser());
        if (UserContext.getCurrentUser().isAdmin()) {
            qo.setEmployeeId(UserContext.getCurrentUser().getId());
            PageInfo<Notice> pageInfo = noticeService.query(qo);
            model.addAttribute("PageInfo", pageInfo);
        } else {
            qo.setRel(true);
            qo.setEmployeeId(UserContext.getCurrentUser().getId());
            PageInfo<Notice> pageInfo = noticeService.query(qo);
            model.addAttribute("PageInfo", pageInfo);
        }

        return "notice/list";
    }


    @RequiredPermission(name = "公告删除", expression = "notice:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            noticeService.delete(id);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name = "公告新增/编辑", expression = "notice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice) {
        if (notice.getId() == null) {
            Date now = new Date();
            notice.setCreateTime(now);
            notice.setCreateUser(UserContext.getCurrentUser());
            noticeService.save(notice);
        } else {
            noticeService.update(notice);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name = "公告新增/编辑", expression = "notice:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("notice", noticeService.get(id));
        }
        return "notice/input";
    }

    @RequestMapping("/show")
    public String show(Long id, Model model) {
        Notice notice = noticeService.get(id);
        readMapper.updateReadStatus(UserContext.getCurrentUser().getId(), notice.getId());
        model.addAttribute("notice", notice);
        return "notice/show";
    }

    @RequiredPermission(name = "公告发布", expression = "notice:push")
    @RequestMapping("/push")
    public String push(Long id) {
        noticeService.updateRelStatus(id, true);
        return "redirect:/notice/list";
    }

    @RequiredPermission(name = "公告取消发布", expression = "notice:remove")
    @RequestMapping("/remove")
    public String remove(Long id) {
        noticeService.updateRelStatus(id, false);
        return "redirect:/notice/list";
    }

    @RequestMapping("/news")
    @ResponseBody
    public List news() {



        NoticeQueryObject qo = new NoticeQueryObject();
        if (UserContext.getCurrentUser().isAdmin()){
            qo.setPageSize(0);
            qo.setEmployeeId(UserContext.getCurrentUser().getId());
            qo.setReadStatus(false);
            PageInfo<Notice> query = noticeService.query(qo);

            return query.getList();
        }
        qo.setPageSize(0);
        qo.setEmployeeId(UserContext.getCurrentUser().getId());
        qo.setReadStatus(false);
        qo.setRel(true);
        PageInfo<Notice> query = noticeService.query(qo);
        return query.getList();
    }
}